package com.example.mycarcalling.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.example.mycarcalling.bean.Guide;
import com.example.mycarcalling.bean.Navigation;
import com.example.mycarcalling.bean.Order;
import com.example.mycarcalling.service.GuideService;
import com.example.mycarcalling.service.OrderService;
import com.example.mycarcalling.service.UserService;
import com.example.mycarcalling.utils.BaseResponse;
import com.example.mycarcalling.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@BaseResponse
@RequestMapping(value = "/guide")
public class GuideController {

    @Resource
    private GuideService guideService;

    @Resource
    private UserService userService;

    @Resource
    private OrderService orderService;


    /**
     * 插入更新一体
     * @param guide id无需传入
     * @param request
     * @return 影响的Guide编号id
     */
    @RequestMapping(method = RequestMethod.POST,value = "/savaGuide")
    public ResponseResult savaGuide(@RequestBody Guide guide, HttpServletRequest request){
        Long resultGuideId;
        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())) {
                if(null == guide){
                    throw new Exception("空的Guide");
                }
                resultGuideId = guideService.savaGuideInfo(guide);
                if (null != resultGuideId) {
                    return new ResponseResult(200, "success", resultGuideId);
                }
                throw new Exception("插入Guide异常");
            }
            throw new Exception("没有司机权限");
        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "/getDriverLocation/{orderId}")
    public ResponseResult getDriverLocation(@PathVariable("orderId") Long orderId){
        Guide guide;
        try {
            if(userService.IsDriver(StpUtil.getLoginIdAsLong())){
                throw new Exception("没有乘客权限");
            }

            guide = guideService.getDriverGuide(orderId);
            if(null != guide){
                return new ResponseResult(200,"success",guide);
            }
            return new ResponseResult(201,"司机还未发起驾车导航",null);

        }catch (NotLoginException e){
            e.printStackTrace();
            return new ResponseResult(503,e.getMessage(),null);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(500,e.getMessage(),null);
        }
    }


}
